package com.victor.controller.app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.User;
import com.victor.entity.UserBankcard;
import com.victor.service.My_bankcardService;
import com.victor.utils.Msg;
import com.victor.utils.ali_bankcard.AlipayBankNoCheck;

/**
 * 银行卡相关
 * 
 * @author
 *
 */
@RestController
@RequestMapping("bankcard")
public class My_bankcardController {
	
	@Autowired
	private My_bankcardService service;

	/**
	 * 通过userid查询银行卡列表
	 * @param userid 用户id
	 * @return
	 */
	@RequestMapping("/getBankcardList")
	public Msg getBankcardList(String userid) {
		if (userid==null || userid.equals("")) {
			return Msg.fail("参数错误");
		}
		List<UserBankcard> list = service.selBankcardListByUserid(userid);
		return Msg.success().add("list", list);
	}
	
	/**
	 *  通过银行卡id查询银行卡详情
	 * @param id 银行卡id
	 * @return
	 */
	@RequestMapping("/bankCardDetail")
	public Msg getBankCardDetailById(Integer id) {
		if (id==null) {
			return Msg.fail("参数错误");
		}
		UserBankcard card = service.bankCardDetail(id); 
		if (card==null) {
			return Msg.fail("银行卡不存在");
		}
		return Msg.success().add("card", card);
	}
	
	/**
	 *  识别银行卡号（TODO）
	 * @param photo 图片路径
	 * @return
	 */
	@RequestMapping("/scanBankCard")
	public Msg getBankCardNumber() throws Exception {
//		  String filepath = FileUpload.uploadFile(file, request, "");
//        FileUpload.deleteFile(filepath, request);
		
        return Msg.success();
	}
	
	/**
	 * 判断输入支付密码是否正确
	 * @param id	用户id
	 * @param inputpaypassword	输入验证的支付密码
	 * @return
	 */
	@RequestMapping("/ifpaypassword")
	public Msg ifPayPassword(Integer id,String inputpaypassword) {
		if(id==null || inputpaypassword==null || inputpaypassword.equals("")) {
			return Msg.fail("参数错误");
		}
		String paypassword = service.ifpaypassword(id);
		if (!inputpaypassword.equals(paypassword)) {
			return Msg.fail("支付密码错误");
		}
		return Msg.success();
	}
	
	/**
	 *  绑定银行卡 TODO -> (银行卡四要素认证)
	 * @param userBankcard
	 * @param phoneNo 预留手机号
	 * @return
	 */
	@RequestMapping("/bindBrandCard")
	public Msg bindBrandCard(UserBankcard userBankcard,String phoneNo) {
		if(userBankcard==null) {
			return Msg.fail("参数错误");
		}
		//通过用户id查询用户真实姓名，身份炸号
		User user = service.selectUserById(userBankcard.getUserid());
		
		//银行卡实名认证四要素
		Map<String, String> bodys = new HashMap<String, String>();
		bodys.put("cardNo", userBankcard.getBankcardnum());//银行卡号
		bodys.put("idNo", user.getIdcard());//身份证
		bodys.put("name", user.getName());//姓名
		bodys.put("phoneNo", phoneNo);//预留手机号
		AlipayBankNoCheck.verification(bodys);
		
		//验证通过添加银行卡
		boolean b = service.bindBrandCard(userBankcard);
		return Msg.judge(b);
	}
	
	/**
	 *  解绑银行卡
	 * @param id 银行卡id
	 * @return
	 */
	@RequestMapping("/unbindBrandCard")
	public Msg unbindBrandCard(Integer id) {
		if(id==null) {
			return Msg.fail("参数错误");
		}
		boolean b = service.unbindBrandCard(id);
		return Msg.judge(b);
	}
}
